<?
require("../db/opendb.php");
require_once('magpierss/rss_fetch.inc');


			$sql = "Select * from Blog_Feeds where Next_Read < '".date('Y-m-d H:i:s')."'";
			$result = mysql_query($sql) or die ("SQL Error".mysql_error());
			while($row=mysql_fetch_assoc($result))
			{
			$rss = fetch_rss( $row['Url'] );
			
				//Get all the channel info
				if($rss->channel['source_encoding'] != '')
				{
					$encoding = $rss->channel['source_encoding'];
				}
				else
				{
					$encoding = 'UTF-8';
				}

				$maintitle = mb_convert_encoding($rss->channel['title'],'HTML-ENTITIES', $encoding); 
				$mainlink = mb_convert_encoding($rss->channel['link'],'HTML-ENTITIES', $encoding); 
				$maindesc = mb_convert_encoding($rss->channel['description'],'HTML-ENTITIES', $encoding); 
				$feed_type = $rss->feed_type;
				$feed_version = $rss->feed_version;
			
			$insert_id_feed = $row['ID'];
			
			$next_read = mktime(date('H')+2, date('i'), date('s'), date('m'), date('d'), date('Y'));
			$next_read = date('Y-m-d H:i:s', $next_read);
			
			$update = "update Blog_Feeds set Last_Read = '".date('Y-m-d H:i:s')."', Next_Read = '".$next_read."' where ID = '".$insert_id_feed."'";
			$iresult = mysql_query($update) or die ("SQL Error".mysql_error());
			
			/*
			$tags2 = explode(",", $tags);
			
			$i = 0;
			while($i < count($tags))
			{
			$insert = "Insert into Blog_Tags (Blog_Feed_ID, Tag, Owner) Values ('".$insert_id_feed."', '".$tags2[$i]."', '".$_SESSION['id']."')";
			$iresult = mysql_query($insert) or die ("SQL Error".mysql_error());
			$i++;
			}
			$i=0;
			*/
			
			//submit all the articles and categories
			
			if($insert_id_feed != "")
			{

				if(strtoupper($feed_type) == "ATOM")
				{
				$count = 0;
					foreach ($rss->items as $item)
					{
						if($item['atom_content'] == "")
						{
							$array = $item['content'];
							$content = $array['encoded'];
						}
						else
						{
							$content = $item['atom_content'];
						}
						
						
						if($item['author_name'] != "")
						{
							$author = $item['author_name'];
						}
						else
						{
							if($item['author'] != "")
								{
									$author = $item['author'];
								}
								else
								{
									$aarray = $item['dc'];
									$author = $aarray['creator'];
								}
						}
						
							$categories = $item['category'];
							$array = $item['dc'];
							$categories .= $array['subject'];
						
						
						$created = date('Y-m-d H:i:s', $item['date_timestamp']);
						
						$c = insert_articles($item['title'], $item['link'], $content, $created, mysql_real_escape_string($author), $categories, $encoding, $insert_id_feed);
						//echo "ATOM:<BR>TITLE: ".$item['title']."<BR>LINK: ". $item['link']."<BR>CONTENT: ".$content."<BR>DATE: ".$created."<BR>AUTHOR: ".$author."<BR>CATEGORIES: ".$item['category']."<BR><BR><BR>";
						$count++;
					}
				}
			
				if(strtoupper($feed_type) == "RSS" && $feed_version == "1.0")
				{
				$count = 0;	
					foreach ($rss->items as $item)
					{
					
						$content = $item['description'];

						if($item['author'] != "")
						{
							$author = $item['author'];
						}
						else
						{
							$aarray = $item['dc'];
							$author = $aarray['creator'];
						}
						

							$categories = $item['category'];
							$array = $item['dc'];
							$categories .= $array['subject'];
						
						
						$created = date('Y-m-d H:i:s', $item['date_timestamp']);
						
						$c = insert_articles($item['title'], $item['link'], $content, $created, mysql_real_escape_string($author), $categories, $encoding, $insert_id_feed);
						//echo "RSS1<BR>TITLE: ".$item['title']."<BR>LINK: ". $item['link']."<BR>CONTENT: ".$content."<BR>DATE: ".$created."<BR>AUTHOR: ".$author."<BR>CATEGORIES: ".$item['category']."<BR><BR><BR>";
						$count++;
					}
				}
				
				if(strtoupper($feed_type) == "RSS" && $feed_version == "2.0")
				{
				$count = 0;
					foreach ($rss->items as $item)
						{

							if($item['atom_content'] == "")
							{
								$array = $item['content'];
								$content = $array['encoded'];
							}
							else
							{
								$content = $item['atom_content'];
							}
							if($item['description'] != "")
							{
								
								$content = $item['description'];
							} 

							if($item['author_name'] != "")
							{
								$author = $item['author_name'];
							}
							else
							{
								if($item['author'] != "")
								{
									$author = $item['author'];
								}
								else
								{
									$aarray = $item['dc'];
									$author = $aarray['creator'];
								}
								
							}
							
							$categories = $item['category'];
							$array = $item['dc'];
							$categories .= $array['subject'];
							
							$created = date('Y-m-d H:i:s', $item['date_timestamp']);
							
							$c = insert_articles($item['title'], $item['link'], $content, $created, mysql_real_escape_string($author), $categories, $encoding, $insert_id_feed);
							//echo "RSS2<BR>TITLE: ".$item['title']."<BR>LINK: ". $item['link']."<BR>CONTENT: ".$content."<BR>DATE: ".$created."<BR>AUTHOR: ".$author."<BR>CATEGORIES: ".$item['category']."<BR><BR><BR>";
							$count++;
						}
						
					}
			}
			
			//log it log.txt
						$filename = 'log.txt';
						$somecontent = $row['Url'].", ".date('Y-m-d H:i:s').", ".$count."\n";
						
						if (is_writable($filename)) 
							{
	
							   // In our example we're opening $filename in append mode.
							   // The file pointer is at the bottom of the file hence 
							   // that's where $somecontent will go when we fwrite() it.
							   if (!$handle = fopen($filename, 'a')) 
							   {
									 exit;
							   }
							
							   // Write $somecontent to our opened file.
							   if (fwrite($handle, $somecontent) === FALSE) 
							   {
								   exit;
							   }
	
							   fclose($handle);
							
							} 

	
}

function insert_articles($title, $link, $description, $date, $author, $categories, $aencoding, $feed_id)
{
	if($date != "")
						{
							$insert_id = "";				
							$insertquery = "insert into Blog_Articles (Link, Title, Blog_Date, Description, Feed_ID, Date_Created, Author) Values ('". mb_convert_encoding($link,'HTML-ENTITIES', $aencoding)."','".mysql_real_escape_string(mb_convert_encoding($title,'HTML-ENTITIES', $aencoding))."','".$date."','".mysql_real_escape_string(mb_convert_encoding($description,'HTML-ENTITIES', $aencoding))."','".$feed_id."','".date('Y-m-d H:i:s')."', '".$author."')";
							mysql_query($insertquery);
							$insert_id = mysql_insert_id();
							//echo $insertquery."<BR><BR>";
							
							//print_r($item['dc:subject']);
							$cats = ltrim($categories, "^");
							$cats = str_replace("^&^", " & ", $cats);
							
							if($insert_id != 0)
							{
								$cats = explode("^", $cats);
									foreach($cats as $cat)
									{
										if ($cat != "")
										{
										$insertquery = "insert into Blog_Article_Categories (Blog_Feed_ID, Category, Article_ID) Values ('".$feed_id."','".mysql_real_escape_string(mb_convert_encoding($cat,'HTML-ENTITIES', $aencoding))."', '".$insert_id."')";
										mysql_query($insertquery);
										//echo $insertquery."<BR><BR>";
										}
									}
							}
					
							//echo "Added article to database<BR><BR>";
							//echo $insertquery;
							$boolean = true;
							
						}
		return $boolean;
}
?>